//
//  ViewController.m
//  20101
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import "StudentModel.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *idtext;
@property (weak, nonatomic) IBOutlet UITextField *nametext;
@property (weak, nonatomic) IBOutlet UITextField *agetext;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath1=paths[0];
    
    NSString *docPath2=[NSHomeDirectory() stringByAppendingString:@"/Documents"];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)save1:(id)sender
{
    NSUserDefaults *d=[NSUserDefaults standardUserDefaults];
    [d setObject:self.idtext.text forKey:@"id"];
    [d setObject:self.nametext.text forKey:@"name"];
    [d setObject:self.agetext.text forKey:@"age"];
    [d synchronize];
}

- (IBAction)load1:(id)sender
{
    NSUserDefaults *d=[NSUserDefaults standardUserDefaults];
    self.idtext.text=[d objectForKey:@"id"];
    self.nametext.text=[d objectForKey:@"name"];
    self.agetext.text=[d objectForKey:@"age"];
}
- (IBAction)save2:(id)sender
{
    NSDictionary *dict=@{@"stuId":self.idtext.text,
                         @"stuName":self.nametext.text,
                         @"stuAge":self.agetext.text};
    NSString *docPath=[NSHomeDirectory() stringByAppendingString:@"/Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"student.plist"];
    [dict writeToFile:filePath atomically:YES];
}
- (IBAction)load2:(id)sender
{
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"student.plist"];
    NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:filePath];
    
    self.idtext.text=dict[@"stuId"];
    self.nametext.text=dict[@"stuName"];
    self.agetext.text=dict[@"stuAge"];
    
    
    
}
- (IBAction)save3:(id)sender
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.arch"];
    
    StudentModel *m = [[StudentModel alloc] initWithId:self.idtext.text
                                               andName:self.nametext.text
                                                andAge:self.agetext.text];
    
    NSMutableData *mData = [NSMutableData data];
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    [arch encodeObject:m forKey:@"Student"];
    [arch finishEncoding];
    
    [mData writeToFile:filePath atomically:YES];

}
- (IBAction)load3:(id)sender
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.arch"];
    
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    
    NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    StudentModel *stu = [unArch decodeObjectForKey:@"Student"];
    [unArch finishDecoding];
    
    self.idtext.text = stu.stuId;
    self.nametext.text = stu.stuName;
    self.agetext.text = stu.stuAge;

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end
